package com.css.bjdt_report.validation.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_report.cell.entity.VarParam;
import com.css.bjdt_report.cell.service.CustomSqlService;
import com.css.bjdt_report.util.CommonUtil;
import com.css.bjdt_report.util.ReportConstant;
import com.css.bjdt_report.validation.entity.ValidationInfo;

@Service
public class ValidationInfoService extends CustomSqlService {
	@Autowired
	private Dao dao;

	/**
	 * 按年度和党组织获取相关校核信息数据
	 * @param annualId 年度id
	 * @param dzzId 党组织id
	 * @return
	 */
	public List<ValidationInfo> getValidationInfoByTemplateId(String annualId, String dzzId) {
		String tableName = ReportConstant.VALIDATION_TABLE_PREFIX + annualId;
		return executeSql(ReportConstant.VALIDATION_QUERY_SQL, new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<ValidationInfo> list = new ArrayList<ValidationInfo>();
				while (rs.next()) {
					ValidationInfo validationInfo = new ValidationInfo();
					validationInfo.setId(rs.getString("ID"));
					validationInfo.setBatchId(rs.getString("BATCH_ID"));
					validationInfo.setSourceExp(rs.getString("SOURCE_EXP"));
					validationInfo.setCellExp(rs.getString("CELL_EXP"));
					validationInfo.setActualExp(rs.getString("ACTUAL_EXP"));
					validationInfo.setValidationType(rs.getString("VALIDATION_TYPE"));
					validationInfo.setValidationState(rs.getString("VALIDATION_STATE"));
					validationInfo.setCellId(rs.getString("CELL_ID"));
					validationInfo.setDzzId(rs.getString("DZZ_ID"));
					list.add(validationInfo);
				}
				return list;
			}
		}, new VarParam("table_name", tableName, "DZZ_ID", dzzId)).getList(ValidationInfo.class);
	}

	/**
	 * 删除
	 * @param annualId 年度id
	 * @param dzzId 党组织id
	 */
	public void deleteValidationInfo(String annualId, String dzzId, String templateId) {
		String tableName = ReportConstant.VALIDATION_TABLE_PREFIX + annualId;
		List<VarParam> varparams = new ArrayList<VarParam>();
		VarParam dzzIdParam = new VarParam(null, null, "dzz_id", dzzId);
		VarParam templateIdParam = new VarParam(null, null, "template_id", templateId);
		VarParam tableNameVar = new VarParam("table_name", tableName, null, null);
		varparams.add(dzzIdParam);
		varparams.add(templateIdParam);
		varparams.add(tableNameVar);
		executeSql(ReportConstant.VALIDATION_DELETE_SQL, null, varparams.toArray(new VarParam[0]));
	}

	/**
	 * 批量插入
	 * @param annualId 年度id
	 * @param validations 校核信息数据集合
	 */
	public void batchInsertValidationInfo(String annualId, List<ValidationInfo> validations) {
		String tableName = ReportConstant.VALIDATION_TABLE_PREFIX + annualId;
		if (validations != null && validations.size() > 0) {
			for (ValidationInfo validation : validations) {
				List<VarParam> varparams = new ArrayList<VarParam>();
				VarParam idParam = new VarParam(null, null, "ID", CommonUtil.getUUID());
				VarParam batchIdParam = new VarParam(null, null, "BATCH_ID", validation.getBatchId());
				VarParam dzzIdParam = new VarParam(null, null, "DZZ_ID", validation.getDzzId());
				VarParam sourceExpParam = new VarParam(null, null, "SOURCE_EXP", validation.getSourceExp());
				VarParam cellExpParam = new VarParam(null, null, "CELL_EXP", validation.getCellExp());
				VarParam actualExpParam = new VarParam(null, null, "ACTUAL_EXP", validation.getActualExp());
				VarParam typeParam = new VarParam(null, null, "VALIDATION_TYPE", validation.getValidationType());
				VarParam stateParam = new VarParam(null, null, "VALIDATION_STATE", validation.getValidationState());
				VarParam cellIdParam = new VarParam(null, null, "CELL_ID", validation.getCellId());
				VarParam templateIdParam = new VarParam(null, null, "TEMPLATE_ID", validation.getTemplateId());
				VarParam tableNameVar = new VarParam("table_name", tableName, null, null);
				varparams.add(idParam);
				varparams.add(batchIdParam);
				varparams.add(sourceExpParam);
				varparams.add(cellExpParam);
				varparams.add(actualExpParam);
				varparams.add(typeParam);
				varparams.add(stateParam);
				varparams.add(cellIdParam);
				varparams.add(tableNameVar);
				varparams.add(dzzIdParam);
				varparams.add(templateIdParam);
				executeSql(ReportConstant.VALIDATION_INSERT_SQL, null, varparams.toArray(new VarParam[0]));
			}
		}
	}
}
